<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="container">
          <el-form size="small" inline label-width="150px">
            <el-form-item :label="$t('orderstatus')">
              <el-radio-group
                v-model="tableFrom.order_type"
                type="button"
                @change="getList(1)"
              >
                <el-radio-button label="">{{ $t("all") }} </el-radio-button>
                <el-radio-button label="1">{{
                  $t("pendingpayment")
                }}</el-radio-button>
                <el-radio-button label="2">{{
                  $t("tobedelivered")
                }}</el-radio-button>
                <el-radio-button label="3">{{
                  $t("pendingreceipt")
                }}</el-radio-button>
                <el-radio-button label="4">{{ $t("comment") }}</el-radio-button>
                <el-radio-button label="5">{{
                  $t("transactioncomplete")
                }}</el-radio-button>
                <el-radio-button label="6">{{
                  $t("refunded")
                }}</el-radio-button>
                <el-radio-button label="7">{{ $t("deleted") }}</el-radio-button>
              </el-radio-group>
            </el-form-item>
            <el-form-item :label="$t('timeselection')" class="width100">
              <el-radio-group
                v-model="tableFrom.date"
                type="button"
                class="mr20"
                size="small"
                @change="selectChange(tableFrom.date)"
              >
                <el-radio-button
                  v-for="(item, i) in fromList.fromTxt"
                  :key="i"
                  :label="item.val"
                  >{{ item.text }}</el-radio-button
                >
              </el-radio-group>
              <el-date-picker
                v-model="timeVal"
                value-format="yyyy/MM/dd"
                format="yyyy/MM/dd"
                size="small"
                type="daterange"
                placement="bottom-end"
                :placeholder="$t('customtime')"
                style="width: 250px"
                @change="onchangeTime"
              />
            </el-form-item>
            <el-form-item :label="$t('keywords')" class="width100">
              <el-input
                v-model="tableFrom.keyword"
                :placeholder="$t('please44')"
                class="selWidth"
                size="small"
                @keyup.enter.native="getList(1)"
              >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  size="small"
                  class="el-button-solt"
                  @click="getList(1)"
                />
              </el-input>
            </el-form-item>
            <el-form-item :label="$t('invoicingstatus')">
              <el-select
                v-model="tableFrom.status"
                :placeholder="$t('pleaseselect')"
                class="filter-item selWidth mr20"
                clearable
                @change="getList"
              >
                <el-option
                  v-for="item in invoiceStatusList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item :label="$t('userinformation')" class="width100">
              <el-input
                v-model="tableFrom.username"
                :placeholder="$t('please95')"
                class="selWidth"
                size="small"
                @keyup.enter.native="getList(1)"
              >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  size="small"
                  class="el-button-solt"
                  @click="getList(1)"
                />
              </el-input>
            </el-form-item>
            <el-button
              style="display: block"
              size="small"
              type="primary"
              @click="getInvoiceInfo('', 'invoice')"
            >
              合并开票
            </el-button>
          </el-form>
        </div>
      </div>
      <el-table
        ref="multipleSelection"
        v-loading="listLoading"
        tooltip-effect="dark"
        :row-key="
          (row) => {
            return row.order_receipt_id;
          }
        "
        :data="tableData.data"
        style="width: 100%"
        class="table"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          :reserve-selection="true"
          width="55"
        />
        <el-table-column
          prop="storeOrder.order_sn"
          :label="$t('ordernumber')"
          min-width="170"
        />
        <el-table-column
          prop="user.nickname"
          :label="$t('usernickname')"
          min-width="90"
        />
        <el-table-column
          prop="order_price"
          :label="$t('orderamount')"
          min-width="90"
        />
        <el-table-column :label="$t('orderstatus')" min-width="90">
          <template slot-scope="scope">
            <span
              v-if="
                scope.row.storeOrder &&
                scope.row.storeOrder.paid === 0 &&
                scope.row.storeOrder.status === 0
              "
              >{{ $t("pendingpayment") }}</span
            >
            <span v-else>{{
              scope.row.storeOrder &&
              scope.row.storeOrder.status | orderStatusFilter
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="receipt_price"
          :label="$t('invoiceamount')"
          min-width="90"
        />
        <el-table-column
          prop="receipt_sn"
          :label="$t('invoiceno')"
          min-width="120"
        />
        <el-table-column :label="$t('invoicetype')" min-width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.receipt_info">{{
              scope.row.receipt_info.receipt_type == 1
                ? $t("plaininvoice")
                : $t("specialinvoice")
            }}</span>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('invoiceheader')" min-width="90">
          <template slot-scope="scope">
            <span>{{
              scope.row.receipt_info.receipt_title_type === "1"
                ? $t("personal")
                : $t("enterprise")
            }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('invoicecontact')" min-width="100">
          <template slot-scope="scope">
            <span>{{
              scope.row.storeOrder && scope.row.storeOrder.real_name
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('invoicecontactinformation')"
          min-width="120"
        >
          <template slot-scope="scope">
            <span>{{
              scope.row.delivery_info.email
                ? scope.row.delivery_info.email
                : scope.row.delivery_info.user_address &&
                  scope.row.delivery_info.user_address +
                    scope.row.delivery_info.user_phone &&
                  scope.row.delivery_info.user_phone
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="create_time"
          :label="$t('ordertime')"
          min-width="120"
        />
        <el-table-column :label="$t('invoicingstatus')" min-width="120">
          <template slot-scope="scope">
            <span>{{ scope.row.status == 1 ? $t("opened") : "未开" }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="mer_mark"
          :label="$t('invoiceremarks')"
          min-width="120"
        />
        <el-table-column
          :label="$t('operation')"
          min-width="180"
          fixed="right"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              v-if="
                scope.row.status === 0 &&
                scope.row.storeOrder &&
                scope.row.storeOrder.paid === 1
              "
              type="text"
              size="small"
              @click="getInvoiceInfo(scope.row.order_receipt_id, 'invoice')"
              >{{ $t("invoicing") }}</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="onOrderDetail(scope.row.order_id)"
              >{{ $t("orderdetails") }}</el-button
            >
            <el-button
              v-if="scope.row.status"
              type="text"
              size="small"
              @click="getInvoiceInfo(scope.row.order_receipt_id, 'edit')"
              >{{ $t("edit") }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
    <!--开票-->
    <el-dialog
      v-if="dialogVisible"
      :title="invoiceInfo.title"
      :visible.sync="dialogVisible"
      width="900px"
    >
      <div v-loading="loading">
        <div class="box-container">
          <div class="acea-row">
            <div class="list sp">
              <label class="name" style="color: #333">{{
                $t("invoicedetails")
              }}</label>
            </div>
            <div class="list sp">
              <label class="name" style="color: #333"
                >{{ $t("invoicerequisitionno") }}：</label
              >{{ invoiceInfo.receipt_sn }}
            </div>
          </div>
          <div class="title">{{ $t("invoiceinformation") }}</div>
          <div class="acea-row">
            <div class="list sp">
              <label class="name">{{ $t("invoiceheader") }}：</label
              >{{ invoiceInfo.receipt_info.receipt_title }}
            </div>
            <div class="list sp">
              <label class="name">{{ $t("invoicetype") }}：</label
              >{{
                invoiceInfo.receipt_info.receipt_type == 1
                  ? $t("plaininvoice")
                  : $t("specialinvoice")
              }}
            </div>
            <div class="list sp">
              <label class="name">发票抬头类型：</label
              >{{
                invoiceInfo.receipt_info.receipt_title_type == "1"
                  ? $t("personal")
                  : $t("enterprise")
              }}
            </div>
            <div class="list sp">
              <label class="name">{{ $t("invoiceamount") }}：</label
              >{{ invoiceInfo.receipt_price }}
            </div>
            <div
              v-if="invoiceInfo.receipt_info.receipt_title_type == '2'"
              class="list sp"
            >
              <label class="name">企业税号：</label
              >{{ invoiceInfo.receipt_info.duty_paragraph }}
            </div>
            <div
              v-if="invoiceInfo.receipt_info.receipt_type == 2"
              class="list sp"
            >
              <label class="name">{{ $t("bankofdeposit") }}：</label
              >{{ invoiceInfo.receipt_info.bank_name }}
            </div>
            <div
              v-if="invoiceInfo.receipt_info.receipt_type == 2"
              class="list sp"
            >
              <label class="name">银行账号：</label
              >{{ invoiceInfo.receipt_info.bank_code }}
            </div>
            <div
              v-if="invoiceInfo.receipt_info.receipt_type == 2"
              class="list sp"
            >
              <label class="name">企业地址：</label
              >{{ invoiceInfo.receipt_info.address }}
            </div>
            <div
              v-if="invoiceInfo.receipt_info.receipt_type == 2"
              class="list sp"
            >
              <label class="name">企业电话：</label
              >{{ invoiceInfo.receipt_info.tel }}
            </div>
          </div>
          <div class="title">联系信息：</div>
          <div
            v-if="invoiceInfo.receipt_info.receipt_type == 1"
            class="acea-row"
          >
            <div class="list sp">
              <label class="name">联系邮箱：</label
              >{{ invoiceInfo.delivery_info.email }}
            </div>
          </div>
          <div v-else class="acea-row">
            <div class="list sp">
              <label class="name">联系人姓名：</label
              >{{ invoiceInfo.delivery_info.user_name }}
            </div>
            <div class="list sp">
              <label class="name">联系人电话：</label
              >{{ invoiceInfo.delivery_info.user_phone }}
            </div>
            <div class="list sp">
              <label class="name">联系人地址：</label
              >{{ invoiceInfo.delivery_info.user_address }}
            </div>
          </div>
          <div class="acea-row">
            <div class="list sp">
              <label class="name">{{ $t("invoicingstatus") }}：</label
              >{{ invoiceInfo.status == 1 ? $t("opened") : "未开" }}
            </div>
            <div class="list sp100">
              <label class="name">发票号码：</label
              ><span class="info"
                ><el-input
                  v-model="invoiceData.number"
                  @keyup.native="
                    invoiceData.number = invoiceData.number.replace(
                      /[^\w]/g,
                      ''
                    )
                  "
              /></span>
            </div>
            <div class="list sp100">
              <label class="name">{{ $t("invoiceremarks") }}：</label
              ><span class="info"
                ><el-input v-model="invoiceData.mark" type="textarea" :rows="5"
              /></span>
            </div>
          </div>
          <el-button
            style="width: 100%; margin-top: 15px"
            type="primary"
            @click="handleInvoic"
            >{{ $t("confirm") }}</el-button
          >
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
import {
  invoiceOrderListApi,
  invoiceUpdateApi,
  invoiceDetailApi,
  invoiceInfoApi,
  invoiceApi,
} from "@/api/order";

export default {
  name: "OrderInvoice",
  data() {
    return {
      logisticsName: "refund",
      id: 0,
      type: "",
      tableData: {
        data: [],
        total: 0,
      },
      invoiceStatusList: [
        { label: this.$t("invoiced"), value: 1 },
        { label: this.$t("uninvoiced"), value: 0 },
      ],
      listLoading: true,
      tableFrom: {
        username: "",
        type: "",
        date: "",
        page: 1,
        limit: 20,
        receipt_sn: "",
        order_type: "",
        keyword: "",
        status: "",
      },
      orderChartType: {},
      timeVal: [],
      multipleSelection: [],
      fromList: {
        title: this.$t("selectionperiod"),
        custom: true,
        fromTxt: [
          { text: this.$t("all"), val: "" },
          { text: this.$t("today"), val: "today" },
          { text: this.$t("yesterday"), val: "yesterday" },
          { text: this.$t("last7days"), val: "lately7" },
          { text: this.$t("last30days"), val: "lately30" },
          { text: this.$t("thismonth"), val: "month" },
          { text: this.$t("thisyear"), val: "year" },
        ],
      },
      selectionList: [],
      tableFromLog: {
        page: 1,
        limit: 20,
      },
      tableDataLog: {
        data: [],
        total: 0,
      },
      loading: false,
      dialogVisible: false,
      orderDatalist: null,
      invoiceInfo: {},
      invoiceData: {
        number: "",
        mark: "",
      },
    };
  },
  mounted() {
    if (this.$route.query.hasOwnProperty("sn")) {
      this.tableFrom.order_sn = this.$route.query.sn;
    } else {
      this.tableFrom.order_sn = "";
    }
    this.getList(1);
  },

  methods: {
    // 订单详情
    onOrderDetail(order_id) {
      this.$router.push({
        name: "OrderList",
        query: {
          id: order_id,
        },
      });
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    pageChangeLog(page) {
      this.tableFromLog.page = page;
      this.getList("");
    },
    handleSizeChangeLog(val) {
      this.tableFromLog.limit = val;
      this.getList("");
    },
    // 开票信息
    getInvoiceInfo(id, type) {
      this.id = id;
      this.type = type;
      const ids = id ? id.toString() : this.getInvoic();
      const params = { ids: ids };
      type === "invoice"
        ? invoiceInfoApi(params)
            .then((res) => {
              this.listLoading = false;
              this.dialogVisible = true;
              this.invoiceInfo = res.data;
              this.invoiceData = {
                number: "",
                mark: "",
              };
            })
            .catch((res) => {
              this.$message.error(res.message);
              this.listLoading = false;
            })
        : invoiceDetailApi(id) // 编辑详情
            .then((res) => {
              this.listLoading = false;
              this.dialogVisible = true;
              this.invoiceInfo = res.data;
              this.invoiceData = {
                number: res.data.receipt_no,
                mark: res.data.mer_mark,
              };
            })
            .catch((res) => {
              this.$message.error(res.message);
              this.listLoading = false;
            });
    },
    // 开票
    handleInvoic() {
      const ids = this.id ? this.id.toString() : this.getInvoic();
      const params = {
        ids: ids,
        receipt_sn: this.invoiceInfo.receipt_sn,
        receipt_price: this.invoiceInfo.receipt_price,
        receipt_no: this.invoiceData.number,
        mer_mark: this.invoiceData.mark ? this.invoiceData.mark : "",
      };
      this.type === "invoice"
        ? invoiceApi(params)
            .then((res) => {
              this.$message.success(res.message);
              this.dialogVisible = false;
              this.getList();
              this.id = "";
            })
            .catch((res) => {
              this.$message.error(res.message);
              this.listLoading = false;
            })
        : invoiceUpdateApi(this.id, {
            receipt_no: this.invoiceData.number,
            mer_mark: this.invoiceData.mark ? this.invoiceData.mark : "",
          })
            .then((res) => {
              this.$message.success(res.message);
              this.dialogVisible = false;
              this.getList();
              this.id = "";
            })
            .catch((res) => {
              this.$message.error(res.message);
              this.listLoading = false;
            });
    },
    // 获取开票订单id
    getInvoic() {
      const ids = [];
      this.multipleSelection.forEach(function (item, index) {
        ids.push(item.order_receipt_id);
      });
      return ids.toString();
    },
    // 编辑
    onOrderMark(id) {
      // this.$modalForm(invoiceorderMarkApi(id)).then(() => this.getList(''))
    },
    // 选择时间
    selectChange(tab) {
      this.tableFrom.date = tab;
      this.timeVal = [];
      this.getList(1);
    },
    // 具体日期
    onchangeTime(e) {
      this.timeVal = e;
      this.tableFrom.date = e ? this.timeVal.join("-") : "";
      this.getList(1);
    },
    // 列表
    getList(num) {
      this.listLoading = true;
      this.tableFrom.page = num || this.tableFrom.page;
      invoiceOrderListApi(this.tableFrom)
        .then((res) => {
          this.orderChartType = res.data.stat;
          this.tableData.data = res.data.list;
          this.tableData.total = res.data.count;
          this.listLoading = false;
        })
        .catch((res) => {
          this.$message.error(res.message);
          this.listLoading = false;
        });
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getList();
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getList();
    },

    handleClose() {
      this.dialogLogistics = false;
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__title {
  font-weight: bold;
}
.selWidth {
  width: 300px;
}
.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
  font-size: 12px;
}
.el-icon-arrow-down {
  font-size: 12px;
}

.tabBox_tit {
  width: 60%;
  font-size: 12px !important;
  margin: 0 2px 0 10px;
  letter-spacing: 1px;
  padding: 5px 0;
  box-sizing: border-box;
}
.box-container {
  overflow: hidden;
  padding: 0 10px;
}
.box-container .title {
  margin: 15px 0 10px;
  color: #333;
  font-weight: bold;
  border-bottom: 1px solid #dfe6ec;
}

.box-container .list {
  float: left;
  line-height: 40px;
}
.box-container .list .info {
  display: block;
  .el-textarea {
    margin-top: 10px;
  }
}

.box-container .sp {
  width: 50%;
}
.box-container .sp3 {
  width: 33.3333%;
}
.box-container .sp100 {
  width: 100%;
}
.box-container .list .name {
  align-items: center;
  width: 100px;
  color: #606266;
}
.acea-row {
  margin-bottom: 25px;
}
</style>
